package hh.proxy.cglib;

import net.sf.cglib.core.DebuggingClassWriter;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author hh
 * @date 2020/9/29
 */
public class CglibDemo {
    public static void main(String[] args) throws Exception{
        System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "c:\\classes");
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(CglibDemo.class);
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
                System.out.println("before...");
                Object result = proxy.invokeSuper(obj, args);
                System.out.println("after ...");
                return result;
            }
        });
        CglibDemo cglibProxy = (CglibDemo) enhancer.create();
        cglibProxy.getName();
    }


    private String name = "hello";

    public String getName() {
        System.out.println("getMethod name...");
        return name;
    }

    public void setName(String name) {
        System.out.println("set name...");
        this.name = name;
    }
}
